package com.chushouya.order.service.api.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.order.constants.*;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
//=====order.constants.*;
import com.chushouya.order.constants.*;
import com.chushouya.order.dao.entity.OrderEntity;
import com.chushouya.order.dao.repository.OrderRepository;
import com.chushouya.order.dto.admin.action.OrderAction;
import com.chushouya.order.dto.admin.order.OrderQuery;
import com.chushouya.order.dto.admin.order.UserOrderAction;
import com.chushouya.order.service.api.UserOrderActionCtrlService;
import com.chushouya.order.util.UserOrderActionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class UserOrderActionCtrlServiceImpl implements UserOrderActionCtrlService {

    @Resource
    private OrderRepository orderRepository;

    private OrderEntity getOrder(String orderNo,Long userId) {
        OrderQuery query = new OrderQuery();
        query.setOrderNo(orderNo);
        query.setUserId(userId);
        OrderEntity orderEntity = orderRepository.selectOne(query);
        if (Objects.isNull(orderEntity)) {
            throw Ex.business("订单不存在");
        }
        return orderEntity;
    }

    /**
     * 订单操作许可验证
     */
    @Override
    public void actionExecuteCheck(UserOrderActionEnum orderAction, OrderEntity orderEntity) {
        OrderStatusEnum orderStatus = OrderStatusEnum.get(orderEntity.getOrderStatus());
        Boolean allowExecute = Objects.nonNull(this.getActionList(orderEntity).stream().filter(f -> Objects.equals(f.getActionCode(), orderAction.getActionCode())).findFirst().orElse(null));
        Check.notFalse(allowExecute, "[{}]状态订单不可操作[{}]", orderStatus.named(), orderAction.named());
    }

    @Override
    public List<UserOrderAction> orderActions(String orderNo) {
        final Long userId = Contexts.getUserContext().getUserId();
//        log.info("用户ID:{},订单号:{}", userId, orderNo);
        return this.getActionList(this.getOrder(orderNo, userId));
    }

    /**
     * 当前订单执行的操作
     */
    private List<UserOrderAction> getActionList(OrderEntity orderEntity) {
        List<UserOrderAction> actions = UserOrderActionUtil.userOrderActions(orderEntity.getOrderStatus());
        List<UserOrderAction> newActions = Lists.newArrayList();
        for (UserOrderAction action : actions) {
            newActions.add(action);
        }
        return newActions;
    }
    /**
     * 重置订单按钮
     */

    private void orderResetStatus(List<OrderAction> actions, OrderEntity orderEntity) {
        final BigDecimal prepaidPrice = orderEntity.getPrepaidPrice();
        final Date createTime = orderEntity.getCreateTime();
        final KefuOrderActionEnum action = KefuOrderActionEnum.RESET;
        if (prepaidPrice.compareTo(BigDecimal.ZERO) > 0) {
            return;
        }
        if (createTime.before(Dates.addMonths(Dates.getTimeNow(), -1))) {
            return;
        }


        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }


    /**
     * 预约按钮
     */
    private void orderReserveActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.RESERVE;

        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 确认发货按钮
     */
    private void orderDeliveryActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.DELIVERY;
        if (Objects.equals(Contexts.getUserContext().getUserType(), ContextUserType.ADMIN_PLATFORM.value())) {
            if (!Lists.asList(OrderPlatformEnum.COMMERCE.value(),OrderPlatformEnum.ALIPAY.value(),OrderPlatformEnum.SUNING.value()).contains(orderEntity.getPlatform())) {
                return;
            }
        }

        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 确认收货按钮
     */
    private void orderReceiveActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.RECEIVE;
        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 退货按钮
     */
    private void orderReturnActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.RETURN;
        final String platform = orderEntity.getPlatform();
        if (Objects.equals(platform, OrderPlatformEnum.COMMERCE.value())) {
            if (!Objects.equals(orderEntity.getOrderStatus(), OrderStatusEnum.WAIT_RETURN.value())) {
                return;
            }
        }
        // 查询是否有上门订单
        if (Objects.equals(OrderTypeEnum.EXPRESS_RECYCLE.value(), orderEntity.getOrderType())) {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

    /**
     * 关闭订单按钮
     */
    private void orderCloseActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.ClOSE;
        final String platform = orderEntity.getPlatform();
        if (Objects.equals(platform, OrderPlatformEnum.COMMERCE.value())) {
            if(!Lists.asList(OrderStatusEnum.NEW_ORDER.value(),OrderStatusEnum.WAIT_CHECK.value()).contains(orderEntity.getOrderStatus())){
                return;
            }
        }
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }


//    /**
//     * 扣款按钮
//     */
//    private void orderDeductionActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
//        final KefuOrderActionEnum action = KefuOrderActionEnum.DEDUCT;
//        // 判断预付款是否大于0
//        if (orderEntity.getPrepaidPrice() != null && orderEntity.getPrepaidPrice().compareTo(BigDecimal.ZERO) > 0) {
//            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
//        }
//    }

    /**
     * 派单按钮
     */
    private void orderDispatchActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.DISPATCH;
        if (Objects.equals(Contexts.getUserContext().getUserType(), ContextUserType.ADMIN_PLATFORM.value())) {
            // 查询是否有上门订单
            if (Objects.equals(OrderTypeEnum.DOOR_RECYCLE.value(), orderEntity.getOrderType())) {
                actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
            }
        } else {
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));

        }

    }


    /**
     * 超时关闭按钮
     */
    private void orderTimeCloseActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.TIMEOUTCLOSE;
        if (Objects.equals(Contexts.getUserContext().getUserType(), ContextUserType.ADMIN_PLATFORM.value())) {
            return;
        }
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }

    /**
     * 收成打款
     */
    private void orderPayActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.PAY;
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }

    /**
     * 完成订单
     */
    private void orderCompleteActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.COMPLETE;
        if(Lists.asList(OrderSourceEnum.CMCC_ONLINE.value()).contains(orderEntity.getOrderSource())){
            return;
        }
        actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
    }
    /**
     * 重发能量
     */
    private void sendEnergyActionCtrl(List<OrderAction> actions, OrderEntity orderEntity) {
        final KefuOrderActionEnum action = KefuOrderActionEnum.SENDENERGY;
        final String platform = orderEntity.getPlatform();
        if (Objects.equals(platform, OrderPlatformEnum.ALIPAY.value())){
            actions.add(new OrderAction(action.getActionCode(), action.getActionName(), action.getActionText()));
        }
    }

}
